<template>
  <div>
   <head-to/>
    <div class="head">
      <van-contact-card
        :type="cheal"
        :name="conner.name"
        :tel="conner.tel"
        @click="onEdit"
      />
    </div>
    <main>
      <div>
        <van-card
          v-for="item in pitchitem" :key="item.goodsId"
          :num="item.number"
          :price="item.price"
          :title="item.name"
          :thumb="item.pic"
        />
      </div>
    </main>
    <van-submit-bar  placeholder :price="price*100" button-text="提交订单" @submit="onSubmit" />
  </div>
</template>

<script>
import { gettoken } from '_utils'
import { mapActions, mapState } from 'vuex'
import { linkmanList, linkmanDetails } from '_api'
import { Toast } from 'vant'
import HeadTo from './components/HeadTo.vue'
export default {
  components: { HeadTo },
  data () {
    return {
      cheal: 'add',
      conner: {
        name: '',
        tel: ''
      }
    }
  },
  created () {
    this.init()
    this.INIT_CART_INFO({
      token: gettoken()
    })
  },
  mounted () {
    console.log(this.$store.state.cart.items)
    console.log(this.pitchitem)
  },
  methods: {
    // 初始联系人判断
    init () {
      linkmanList({
        token: gettoken()
      }).then((res) => {
        if (res.data.code === 0) {
          this.conner = res.data.data.result
          if (this.conner.length === 0) {
            this.cheal = 'add'
          } else {
            // 判断从哪来
            if (this.$route.query.id) {
              linkmanDetails({
                id: this.$route.query.id,
                token: gettoken()
              }).then((res) => {
                console.log(res, 'gg')
                if (res.data.code === 0) {
                  this.conner.name = res.data.data.info.linkMan
                  this.conner.tel = res.data.data.info.mobile
                  this.cheal = 'edit'
                }
              })
            } else {
              res.data.data.result.forEach(item => {
                if (item.isDefault) {
                  this.conner.name = item.linkMan
                  this.conner.tel = item.mobile
                  this.cheal = 'edit'
                }
              })
            }
          }
        }
      })
    },
    onSubmit () {
      Toast('没钱做接口了意思一下')
    },
    onEdit () {
      this.$router.push('/linkman')
    },
    ...mapActions('cart', ['INIT_CART_INFO'])
  },
  computed: {
    ...mapState({
      items: (state) => state.cart.items,
      price: (state) => state.cart.price
    }),
    pitchitem () {
      const pitchitem = []
      this.items.forEach((item) => {
        if (item.selected) {
          pitchitem.push(item)
        }
      })
      return pitchitem
    }
  }
}
</script>

<style lang="scss" scoped>
main{
    margin-bottom: 1.6667rem;
}
</style>
